package zhou.chs.p2_12;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.TextView;

public class Main2Activity extends AppCompatActivity implements  SeekBar.OnSeekBarChangeListener{
    TextView tv3;  //标签
    EditText et;    //用EditText控件设定开度值
    SeekBar sb;     //用SeekBar控件设定开度值
    Button bt3;     //设定值确认，返回主界面
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        tv3=(TextView)findViewById(R.id.idtv3);
        et=(EditText)findViewById(R.id.idet);
        sb=(SeekBar)findViewById(R.id.seekBar);
        bt3=(Button)findViewById(R.id.idbt3);
        sb.setOnSeekBarChangeListener(this);    //注册SeekBar变化事件监听
        Intent it = getIntent();    //获取传入参数
        String s = it.getStringExtra("设备");
        int m = it.getIntExtra("开度",0);
        tv3.setText(s + "开度设定");    //显示传入参数
        et.setText(Integer.toString(m));
        sb.setProgress(m);
    }
    //按钮单击响应
    public void setbn(View view) {
        int n;
        Intent it2 = new Intent();
        String s=et.getText().toString();  //EditText输入内容转string
        if(s.length()==0) n=0;              //未输入数据时设定值为0
        else n=Integer.parseInt(s);        //string转int
        if((n<0)||(n>100)) n=0;            //设定值超范围默认为0
        it2.putExtra("开度",n);    //返回主界面传回开度设定值
        setResult(RESULT_OK,it2);
        finish();
    }
    @Override  //SeekBar变化事件响应
    public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
        et.setText(Integer.toString(i));
    }
    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
    }
    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
    }
}
